package cn.sanch.system.controller;

import cn.sanch.basic.util.AjaxResult;
import cn.sanch.basic.util.PageList;
import cn.sanch.system.domain.SystemDictionaryType;
import cn.sanch.system.query.SystemDictionaryTypeQuery;
import cn.sanch.system.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：数据字典类型
 * @author sanch37
 * 2022/5/28
 */
@Api(value = "宠物之家的数据字典类型相关API", description = "数据字典类型的CRUD操作")
@RestController
@RequestMapping("/systemDictionaryType")
public class SystemDictionaryTypeController {

    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;

    /**
     * 接口：查询数据字典类型单个对象
     * @param id Long id
     * @return SystemDictionaryType
     */
    @ApiOperation(value = "查询数据字典类型单个对象")
    @GetMapping("/{id}")
    public SystemDictionaryType queryOne(@PathVariable Long id) {
        return systemDictionaryTypeService.findOne(id);
    }


    /**
     * 接口：查询数据字典类型所有对象
     * @return List<SystemDictionaryType>
     */
    @ApiOperation(value = "查询数据字典类型所有对象")
    @GetMapping
    public List<SystemDictionaryType> queryAll() {
        return systemDictionaryTypeService.findAll();
    }


    /**
     * 接口：删除单个数据字典类型对象
     * @param id Long id
     * @return AjaxResult
     */
    @ApiOperation(value = "删除单个数据字典类型对象")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable Long id) {
        try {
            systemDictionaryTypeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！");
        }
    }

    /**
     * 接口：高级查询 + 分页查询
     * @param systemDictionaryTypeQuery SystemDictionaryTypeQuery systemDictionaryTypeQuery
     * @return PageList<SystemDictionaryType>
     */
    @ApiOperation(value = "高级查询 + 分页查询")
    @PostMapping
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery systemDictionaryTypeQuery) {
        return systemDictionaryTypeService.queryPage(systemDictionaryTypeQuery);
    }

    /**
     * 接口：数据字典类型添加或修改
     * @param systemDictionaryType
     * @return
     */
    @ApiOperation(value = "数据字典类型添加或修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType) {
        try {
            /* 没有id为添加 */
            if (systemDictionaryType.getId() == null) {
                systemDictionaryTypeService.save(systemDictionaryType);
            /* 有id为修改 */
            }else {
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
     * 接口：批量删除数据字典类型
     * @param ids Long[] ids
     * @return AjaxResult
     *
     *
     */
    @ApiOperation(value = "批量删除数据字典类型")
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            systemDictionaryTypeService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

}
